package com.hzhr.iot.log.dao;

import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.log.dao.mapper.custom.LogEntityCustomMapper;
import com.hzhr.iot.log.entity.LogEntityWithBLOBs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class LogDao {
    @Autowired
    LogEntityCustomMapper logEntityCustomMapper;

    public int insert(LogEntityWithBLOBs record) {
        if (record == null) return 0;

        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());

        Date date = new Date();
        String day = HzhrDateUtil.dateToStr(date, "yyyyMMdd");

        return logEntityCustomMapper.insert(day, record);
    }

    public int update(LogEntityWithBLOBs record) {
        if (record == null) return 0;

        record.setUpdateTime(new Date());

        Date date = new Date();
        String day = HzhrDateUtil.dateToStr(date, "yyyyMMdd");

        return logEntityCustomMapper.update(day, record);
    }

    public LogEntityWithBLOBs selectWithBLOBSByTransactionIdAndServiceName(String transactionId,
                                                                                 String serviceName) {
        Date date = new Date();
        String day = HzhrDateUtil.dateToStr(date, "yyyyMMdd");

        return logEntityCustomMapper.selectWithBLOBSByTransactionIdAndServiceName(day, transactionId, serviceName);
    }
}
